//
//  logInViewController.m
//  GaKan
//
//  Created by mac on 17/9/17.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "logInViewController.h"


@interface logInViewController ()
//全局变量
{
    UITextField *phonenumber;
    UITextField *password;
}
@end

@implementation logInViewController

//重写init函数
-(instancetype)init{
    self = [super init];
    if (self)
    {
        [self setToptittle:@"登录账号"];
        [self setTopTittleColor:[UIColor whiteColor]];
        [self setTopBgColor:headColor];
        [self setBackBtnImage:[UIImage imageNamed:@"wo_merchandise_detail_back@3x"]];
        [self setBackBtnHide:NO];
        
    }
    return self;
}
//主函数 设置ui界面
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    UIImageView *headimage=[[UIImageView alloc]initWithFrame:CGRectMake((SCREENWIDTH-80)/2, 150, 80, 80)];
    headimage.image=[UIImage imageNamed:@"app_logo"];
    [self.view addSubview:headimage];
    phonenumber = [[UITextField alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(headimage.frame)+40, SCREENWIDTH-40, 40)];
    phonenumber.returnKeyType = UIReturnKeyDone; //设置按键类型
    phonenumber.keyboardType=UIKeyboardTypeNumberPad;
    phonenumber.enablesReturnKeyAutomatically = NO;
    phonenumber.textColor = [UIColor blackColor];
    phonenumber.font = [UIFont systemFontOfSize:14];
    phonenumber.textAlignment = NSTextAlignmentLeft;
    phonenumber.placeholder = @"请输入账号";
    phonenumber.secureTextEntry = NO;
    phonenumber.borderStyle = UITextBorderStyleNone;
    [self.view addSubview:phonenumber];
    UIView *line1=[[UIView alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(phonenumber.frame), SCREENWIDTH-40, 1)];
    line1.backgroundColor=headColor;
    [self.view addSubview:line1];
    
    password = [[UITextField alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(line1.frame)+20, SCREENWIDTH-40, 40)];
    password.returnKeyType = UIReturnKeyDone; //设置按键类型
    password.keyboardType = UIKeyboardTypeNumberPad;
    password.enablesReturnKeyAutomatically = NO;
    password.textColor = [UIColor blackColor];
    password.font = [UIFont systemFontOfSize:14];
    password.textAlignment = NSTextAlignmentLeft;
    password.placeholder = @"请输入密码";
    password.secureTextEntry = YES;
    password.borderStyle = UITextBorderStyleNone;
    [self.view addSubview:password];
    UIView *line2=[[UIView alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(password.frame), SCREENWIDTH-40, 1)];
    line2.backgroundColor=headColor;
    [self.view addSubview:line2];
    
    UIButton *takein=[[UIButton alloc]initWithFrame:CGRectMake(40, CGRectGetMaxY(line2.frame)+40, SCREENWIDTH-80, 40)];
    [takein setTitle:@"登录" forState:UIControlStateNormal];
    takein.titleLabel.font = [UIFont systemFontOfSize: 13.0];
    takein.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;
    [takein setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    takein.backgroundColor=headColor;
    takein.layer.cornerRadius=12;
    [takein addTarget:self action:@selector(BTNtakein) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:takein];
    
}

//登录
//按钮事件
-(void)BTNtakein{
    [self gain];
}
//数据获取
-(void)gain{
    BmobQuery *bquery = [BmobQuery queryWithClassName:@"user"];
    
    [bquery whereKey:@"user_login_id" equalTo:phonenumber.text];
    [bquery whereKey:@"user_login_code" equalTo:password.text];
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        if (!error&&array.count!=0) {
            for (BmobObject *obj in array) {
                //打印playerName
                NSLog(@"obj.playerName = %@", [obj objectForKey:@"user_id"]);
                NSLog(@"obj.objectId = %@", [obj objectId]);

                //将个人信息存入沙盒
                [[NSUserDefaults standardUserDefaults]setObject:@"1" forKey:@"is_login"];
                [[NSUserDefaults standardUserDefaults]setObject:[obj objectForKey:@"user_id"] forKey:@"user_id"];
                [[NSUserDefaults standardUserDefaults]setObject:[obj objectForKey:@"user_login_id"] forKey:@"user_login_id"];
                [[NSUserDefaults standardUserDefaults]setObject:[obj objectForKey:@"user_login_code"] forKey:@"user_login_code"];
                [[NSUserDefaults standardUserDefaults]setObject:[obj objectForKey:@"user_name"] forKey:@"user_name"];
                [[NSUserDefaults standardUserDefaults]setObject:[obj objectForKey:@"user_department"] forKey:@"user_department"];
                [[NSUserDefaults standardUserDefaults]setObject:[obj objectForKey:@"user_job"] forKey:@"user_job"];
                [[ViewManager shareInstance].NavigationController popViewControllerAnimated:YES];
            }
        }else{
            DSToast *toast=[[DSToast alloc]initWithText:[NSString stringWithFormat:@"%@",error]];
            [toast showInView:self.view];
        }
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
